package com.example.greendao;

import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;

import com.example.greendao.dao.DaoMaster;
import com.example.greendao.dao.DaoMaster.DevOpenHelper;
import com.example.greendao.dao.DaoSession;
import com.example.greendao.dao.Note;
import com.example.greendao.dao.NoteDao;

import de.greenrobot.dao.query.QueryBuilder;

public class ItemListActivity extends Activity {
	 private SQLiteDatabase db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_item_list);
		initView();

	}

	private void initView() {
		DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "zhudi-db", null);
		db = helper.getReadableDatabase();
		DaoMaster daoMaster = new DaoMaster(db);
		DaoSession daoSession = daoMaster.newSession();
		NoteDao noteDao = daoSession.getNoteDao();
		//插入 可以
		Note note = new Note(null, "1", "12123", new Date()); 
		noteDao.insert(note); 
		//查询全部 可以
		List<Note> list = noteDao.loadAll();//loadAllDeepFromCursor
		System.out.println(list);
		//删除全部 可以
		noteDao.deleteAll(); 
		//查询某条数据 可以
		QueryBuilder qb = noteDao.queryBuilder();
		//qb.where(NoteDao.Properties.Comment.eq("12123")).orderAsc(NoteDao.Properties.Id).build();//升序
		List<Note> queryList = qb.where(NoteDao.Properties.Comment.eq("12123")).orderDesc(NoteDao.Properties.Id).build().list();//降序
		System.out.println(queryList);
		//查询某个表是否包含某个id:
		QueryBuilder qbHas = noteDao.queryBuilder();
		int count = (int) qbHas.where(NoteDao.Properties.Comment.eq("xx")).buildCount().count();
		if (count > 0) {
			Log.d("查询某个表是否包含某个id", "true");
		} else {
			Log.d("查询某个表是否包含某个id", "false");
		}
		//查找
		Note noteByRow = noteDao.loadByRowId(1);
		Log.d("loadByRowId", noteByRow.toString());
		//更新
		Note noteUpdate = new Note(null, "1", "12123", new Date()); 
		noteDao.update(noteUpdate);
		//删除某数据
		qb.where(NoteDao.Properties.Comment.eq("12123")).buildDelete();
		//删除某数据
		QueryBuilder qbDelete = noteDao.queryBuilder();
		qb.where(NoteDao.Properties.Comment.eq("12123")).build();
		List<Note> queryListDelete = qb.list();
		noteDao.deleteInTx(queryListDelete); 
	}
}
